﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GTAuto.DAL.DAL;
using GTAuto.DAL.Entities;

namespace GTauto.Site.FrontEnd.Controllers
{
    public class AutoModelController : Controller
    {
        //
        // GET: /AutoModel/

        public ActionResult Model(int id)
        {
            GTModel model = DALModels.Singleton.Get(id);

            var gtModelModifications = new List<GTModelModification>();
            var firtItem = new GTModelModification { Id = 0, Name = "Выберите компл." };

            gtModelModifications.Add(firtItem);
            gtModelModifications.AddRange(DALModelModification.Singleton.GetAll(model.Id));


            ViewBag.Mods1 = new SelectList(gtModelModifications, "Id", "Name", gtModelModifications);
            ViewBag.Mods2 = new SelectList(gtModelModifications, "Id", "Name", gtModelModifications);
            ViewBag.Mods3 = new SelectList(gtModelModifications, "Id", "Name", gtModelModifications);

            if (gtModelModifications.Count > 1)
            {
                ViewBag.Mods1 = new SelectList(gtModelModifications, "Id", "Name", gtModelModifications[1].Id);
            }

            if (gtModelModifications.Count > 2)
            {
                ViewBag.Mods2 = new SelectList(gtModelModifications, "Id", "Name", gtModelModifications[2].Id);
            }


            if (gtModelModifications.Count > 3)
            {
                ViewBag.Mods3 = new SelectList(gtModelModifications, "Id", "Name", gtModelModifications[3].Id);
            }


            List<GTModelModificationProperty> gtModelModificationProperties = DALModelModificationProperty.Singleton.GetByModel(id);


            var activeCategories = new List<GTPropertyCategory>();

            string props = "";
            foreach (var gtModelModificationProperty in gtModelModificationProperties)
            {
                props = props + "_" + gtModelModificationProperty.PropertyId;

                GTPropertyCategory category = DALPropertyCategory.Singleton.Get(gtModelModificationProperty.ModificationProperty.Category);
                bool needToAdd = true;
                foreach (var it in activeCategories)
                {
                    if (it.Id == category.Id) needToAdd = false;
                }

                if (needToAdd)
                    activeCategories.Add(category);
            }
            ViewBag.ActivCategories = activeCategories;
            ViewBag.ModelModificationProperties = gtModelModificationProperties;
            ViewBag.props = props;
            return View(model);
        }


        public ActionResult ModelsVertical(int id)
        {
            int count;
            List<GTModel> gtModels = DALModels.Singleton.GetByBrand(id, 1, 1000, out count);

            return View(gtModels);
        }


        public JsonResult ModelModificationPropertiesGet(int id)
        {
            List<SimpleObject> gtModelModificationProperties =
                DALModificationPropertyValue.Singleton.GetbyModelSimple(id);
            return Json(gtModelModificationProperties);
        }
    }
}
